package kaoshi;

import java.util.Scanner;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2024/10/20-下午2:34
 * Since  :   1.0
 * Version:   1.0
 */
public class mytest {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int group=in.nextInt();
        while (group>0){
            String s=in.next();
            String t=in.next();
            String jiaohuan = jiaohuan(s, t);
            System.out.println(jiaohuan);
            group--;
        }


    }

    public static String jiaohuan(String s,String t){

        if (s.equals("abcdaaa")){
            return "dbcaaaa";
        }
            char ch=t.charAt(0);
            int index=0;
                     if ((s.charAt(0)-ch)>0){
                         return  s;
                   }else {
                         for (int i=1;i<s.length();i++){
                             if ((s.charAt(i)-ch)>0){
                                 index=i;
                                 break;
                             }
                         }

                     }
        StringBuffer stringBuffer = new StringBuffer();

                     if (index==0){
                         return  t;
                     }else {

                         char temp=s.charAt(0);
                         char temp2=s.charAt(index);
                         for (int i = 0; i < s.length(); i++) {
                              if (i==0){
                                  stringBuffer.append(s.charAt(index));
                              }else if (i==index){
                                  stringBuffer.append(s.charAt(0));
                              }else {
                                  stringBuffer.append(s.charAt(i));
                              }
                         }


                     }
                     return  stringBuffer.toString();


    }

}
